package com.example.malltest.client.service.impl;


import com.example.malltest.client.entity.Client;
import com.example.malltest.client.entity.ClientDTO;
import com.example.malltest.client.entity.ClientDTO1;
import com.example.malltest.client.entity.ClientPwd;
import com.example.malltest.client.mapper.ClientMapper;
import com.example.malltest.client.service.ClientService;
import com.example.malltest.common.ServiceCode;
import com.example.malltest.common.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class ClientServiceImpl implements ClientService {
    @Autowired
    private ClientMapper clientMapper;

    /**
     * 登录
     * @param clientPwd
     * @return
     */
    @Override
    public Client login(ClientPwd clientPwd){
        Client client = clientMapper.getByName(clientPwd.getName());
        if(client == null){
            throw new ServiceException(ServiceCode.ERR_EXISTS,"不存在客户！");
        }
        if(!client.getPassword().equals(clientPwd.getPassword())){
            throw new ServiceException(ServiceCode.ERR_BAD_REPWD,"密码不一致！");
        }
        return client;
    }


    /**
     * 客户列表
     * @return
     */
    @Override
    public List<Client> getList(){
      List<Client> clients = clientMapper.getList();
      if(clients == null){
          throw new ServiceException(ServiceCode.ERR_EXISTS,"不存在客户！");
      }
      return clients;
  }

    /**
     * 增加客户
     * @param clientDTO
     */
    @Override
    public void add(ClientDTO clientDTO){
        Client client = new Client();
        client.setName(clientDTO.getName());
        client.setAge(clientDTO.getAge());
        client.setGender(clientDTO.getGender());
        client.setAddress(clientDTO.getAddress());
        client.setStatus(clientDTO.getStatus());
        client.setPhone(clientDTO.getPhone());
        client.setPassword("123");
        int result = clientMapper.save(client);
        if(result<0){
            throw new ServiceException(ServiceCode.ERR_SAVE_FAILED,"增加客户失败！");
        }
    }

    /**
     * 删除客户
     * @param id
     */
    @Override
    public void delete(Integer id){
       Client client = clientMapper.getById(id);
       if(client == null){
           throw new ServiceException(ServiceCode.ERR_EXISTS,"不存在客户！");
       }
       if(client.getStatus().equals("激活")){
           throw new ServiceException(ServiceCode.ERR_EXISTS,"该客户状态激活中！");
       }
        int result = clientMapper.delete(client);
        if(result<0){
            throw new ServiceException(ServiceCode.ERR_SAVE_FAILED,"增加客户失败！");
        }
    }

    /**
     * 编辑客户
     * @param id
     * @param clientDTO1
     */
    @Override
    public void edit(Integer id, ClientDTO1 clientDTO1){
        Client client = clientMapper.getById(id);
        if(client == null){
            throw new ServiceException(ServiceCode.ERR_EXISTS,"不存在客户！");
        }
        client.setName(clientDTO1.getName());
        client.setAge(clientDTO1.getAge());
        client.setGender(clientDTO1.getGender());
        client.setAddress(clientDTO1.getAddress());
        client.setPhone(clientDTO1.getPhone());
        int result = clientMapper.edit(client);
        if(result<0){
            throw new ServiceException(ServiceCode.ERR_SAVE_FAILED,"增加客户失败！");
        }
    }


    /**
     * 更改状态
     * @param id
     * @param status
     */
    @Override
    public void status(Integer id,String status){
        Client client = clientMapper.getById(id);
        if(client == null){
            throw new ServiceException(ServiceCode.ERR_EXISTS,"不存在客户！");
        }
        client.setStatus(status);
        int result = clientMapper.edit(client);
        if(result<0){
            throw new ServiceException(ServiceCode.ERR_SAVE_FAILED,"增加客户失败！");
        }
    }

    /**
     * 获取客户信息
     * @param name
     * @return
     */
    public Client getClient(String name){
        Client client = clientMapper.getByName(name);
        if(client == null){
            throw new ServiceException(ServiceCode.ERR_EXISTS,"不存在该客户");
        }
        return client;
    }

    /**
     * 修改个人信息
     * @param name
     * @param client
     */
    public void change(String name, Client client){
        Client client1 = clientMapper.getByName(name);
        if(client1 == null){
            throw new ServiceException(ServiceCode.ERR_EXISTS,"不存在该客户");
        }
        client1.setName(client.getName());
        client1.setPhone(client.getPhone());
        client1.setGender(client.getGender());
        client1.setAge(client.getAge());
        client1.setAddress(client.getAddress());
        int result = clientMapper.edit(client1);
        if(result<0){
            throw new ServiceException(ServiceCode.ERR_FILE_UPLOAD,"更新失败！");
        }
    }

    /**
     * 修改密码
     * @param name
     * @param OldPassword
     * @param NewPassword
     */
    public void updatePwd(String name, String OldPassword, String NewPassword){
        Client client = clientMapper.getByName(name);
        if(client == null){
            throw new ServiceException(ServiceCode.ERR_EXISTS,"不存在该客户");
        }
        if(!client.getPassword().equals(OldPassword)){
            throw new ServiceException(ServiceCode.ERR_EXISTS,"旧密码不正确！");
        }
        client.setPassword(NewPassword);
        int result = clientMapper.edit(client);
        if(result<0){
            throw new ServiceException(ServiceCode.ERR_FILE_UPLOAD,"更新失败！");
        }
    }


}
